package com.hdax.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdax.entity.CusDevelop;
import com.hdax.entity.CusOpportunity;
import com.hdax.entity.Customer;
import com.hdax.service.CusDevelopService;
import com.hdax.service.CusOpportunityService;
import com.hdax.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * <p>
 * 跟进表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/cusDevelop")
public class CusDevelopController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CusDevelopService cusDevelopService;

    @Autowired
    private CusOpportunityService cusOpportunityService;

    @GetMapping("/toCusDev")
    public ModelAndView toCusDev() {
        return new ModelAndView("cus_dev/cus_dev");
    }

    @GetMapping("/toCusDevData")
    public ModelAndView toCusDevData(Integer id) {
        ModelAndView modelAndView = new ModelAndView("cus_dev/cus_dev_data");
        Customer customer = customerService.findCustomerById(id);
        System.out.println(customer);
        modelAndView.addObject("customer", customer);
        return modelAndView;
    }

    @PostMapping("/list")
    public IPage<CusDevelop> list(Integer page, Integer limit, Integer id) {
        return cusDevelopService.findDevelopById(new Page<>(page, limit), id);
    }

    @GetMapping("/toCusDevInsert")
    public ModelAndView toCusDevInsert(Integer id) {
        ModelAndView modelAndView = new ModelAndView("cus_dev/cus_dev_insert");
        modelAndView.addObject("id", id);
        return modelAndView;
    }


    @PostMapping("/insert")
    public String insert(CusDevelop cusDevelop) {
        cusDevelop.setCreateTime(LocalDateTime.now());
        cusDevelopService.save(cusDevelop);
        return "success";
    }

    @GetMapping("/toCusOpportunityDev")
    public ModelAndView toCusOpportunityDev(Integer id) {
        ModelAndView modelAndView = new ModelAndView("opportunity/cus_opportunity_dev");
        Customer customer = customerService.findCustomerById(id);
        System.out.println(customer);
        modelAndView.addObject("customer", customer);
        return modelAndView;
    }

    @GetMapping("/toCusOpportunityDevInsert")
    public ModelAndView toCusOpportunityDevInsert(Integer id) {
        ModelAndView modelAndView = new ModelAndView("opportunity/cus_opportunity_dev_insert");
        modelAndView.addObject("id", id);
        return modelAndView;
    }

    @PostMapping("/opportunityInsert")
    public String opportunityInsert(CusDevelop cusDevelop) {
        LocalDateTime localDateTime = LocalDateTime.now();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("cus_id",cusDevelop.getCusId());
        CusOpportunity cusOpportunity = cusOpportunityService.getOne(queryWrapper);
        cusOpportunity.setOpportunityState(2);
        cusOpportunity.setUpdateTime(localDateTime);
        cusOpportunityService.updateById(cusOpportunity);
        cusDevelop.setCreateTime(localDateTime);
        cusDevelopService.save(cusDevelop);
        return "success";
    }


}

